InScribus=0 
################################
# Questo script per Scribus crea una guida TV stampabile dei programmi
# SKY di propria scelta, sulla base dei file di output prodotti dal
# programma jTV-grab.
# 
# Realizzato a partire dallo script di creazione agende dello stesso autore.
#
# Realizzato da Luca Cassioli 2009
############################### 

VERSION = "0.0.1"
COLUMNS = 3
ROWS = 8
MINLEN = 10

OutputType = 'film'

x=0
y=0
HEIGHT = 0
YDIST=0
WIDTH=0
XDIST=0


import sys
import calendar
import datetime
    
    
    
DayName = [u'Lunedo', u'Martedo', u'Mercoledo', u'Giovedo', u'venerdo', u'Sabato', u'Domenica']
MonthName = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre']

try:
    from scribus import *
    InScribus = 1
except ImportError:
    InScribus = 0
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    #sys.exit(1)   
    
           
if (InScribus==1):
  if newDoc(PAPER_A4, (10, 10, 10, 10), PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT):   
    page = getPageSize()
    marg = getPageMargins()  
  else:
    sys.exit(1)
else:
  page=[210,290]
  marg = [10,10,10,10]  
  
PageWidth = page[0]
PageHeight = page[1]/2   
TopMargin = marg[0]
LeftMargin = marg[1]
RightMargin = marg[2]
BottomMargin = marg[3]

DiaryPageWidth = PageWidth/2 - 2*LeftMargin     
 
Event1Height = 10    
Event1Width = DiaryPageWidth/2
Event1FillColor = "White"
Event1LineColor = "Black"
Event1LineWidth = 1
Event1FontSize = 6

Event2Height = 45    
Event2Width = DiaryPageWidth/2
Event2FillColor = "White"
Event2LineColor = "Black"
Event2LineWidth = 1
Event2FontSize = 6

Event3Height = 24
Event3Width = DiaryPageWidth/2
Event3FillColor = "White"
Event3LineColor = "Black"
Event3LineWidth = 1
Event3FontSize = 6

WIDTH = 185
HEIGHT = Event1Height + Event2Height + Event3Height
XDIST=9
YDIST=8
YOFFSET=30  

HeaderTop =  TopMargin
HeaderLeft =  LeftMargin
HeaderWidth = PageWidth - LeftMargin - RightMargin
HeaderHeight = YOFFSET-5

FooterTop = (ROWS)*(HEIGHT+YDIST)+TopMargin+YOFFSET 
FooterLeft = LeftMargin
FooterWidth = PageWidth-RightMargin-LeftMargin
FooterHeight =  80    
  
def CreaFilmOutput():
  global FileContents
  global HeaderTop, HeaderLeft, HeaderWidth, HeaderHeight, FooterWidth, FooterHeight
  EventNum=1
  EOF=0
  PagesCount=0
  while (len(FileContents)>10):  # DEBUG
    PagesCount=PagesCount+1
    if (InScribus==1):
      newPage(-1)
      gotoPage(PagesCount)
    for y in range (0,ROWS): 
      if len(FileContents)<MINLEN:
        break
      for x in range(0,COLUMNS): 
        if len(FileContents)<MINLEN:
          break                    
        L,EOL=ReadLine()
        Title,Plot,Channel,FirstTime,Times = EstraiDati(L,EOL)        
        CreaRettangoliFilm(x,y,Title,Plot,Channel,FirstTime,Times)
        EventNum=EventNum+1
        FileContents=FileContents[EOL+1:len(FileContents)]
        if (len(FileContents)<MINLEN): 
          x=COLUMNS
          y=ROWS
          break   
    StampaHeaderFooter(PagesCount)


def CreaRettangoliFilm(x,y,Title,Plot,Channel,FirstTime,Times):
  Event1Top, Event1Left, Event2Top , Event2Left, Event3Top, Event3Left = AssegnaCoordinateRettangoli(x,y)
  print Event1Top, Event1Left, Event2Top , Event2Left, Event3Top, Event3Left
  if (InScribus==1):
   # Titolo
    tempText1 = createText(Event1Left,Event1Top,WIDTH,Event1Height)
    setLineWidth(Event1LineWidth, tempText1)
    setFillColor(Event1FillColor, tempText1)
    setLineColor(Event1LineColor, tempText1)
    setFontSize(Event1FontSize,tempText1)       
    setText(Title,  tempText1)     
    
   # Trama
    tempText2 = createText(Event2Left,Event2Top,WIDTH,Event2Height)
    setLineWidth(Event2LineWidth, tempText2)
    setFillColor(Event2FillColor, tempText2)
    setLineColor(Event2LineColor, tempText2)
    setFontSize(Event2FontSize,tempText2)
    setText(Plot,tempText2)  
    
   # Passaggi
    tempText3 = createText(Event3Left,Event3Top,WIDTH,Event3Height)
    setLineWidth(Event3LineWidth, tempText3)
    setFillColor(Event3FillColor, tempText3)
    setLineColor(Event3LineColor, tempText3)
    setFontSize(Event3FontSize,tempText3)   
    setText(Channel+", "+ FirstTime + ", " + Times,tempText3)    
  else:
    print Title
    print Plot
    print Channel+", "+ FirstTime + ", " + Times
  
  
def ReadLine():
  global FileContents
#Giorno	Canale	Event id			Inizio	Durata	Titolo	Titolo normalizzato	Descrizione breve	Genere	Sottogenere	PrimaTV#passaggi
  EOL=FileContents.find("\n") # Trova fine lina nella stringa che contiene l'intero file.    
  Line=FileContents[0:EOL+1]  # "Legge" la linea.
  deb=0
  # Continua a leggere linee finche' non ne trova una che contiene un film
  while (Line.find("\tfilm\t") == -1) and (deb<1000) and (len(FileContents)>10): #DEBUG
    deb=deb+1
    FileContents=FileContents[EOL+1:len(FileContents)]
    EOL=FileContents.find("\n")
    Line=FileContents[0:EOL+1]
  if (Line.find("\tfilm\t") == -1):
    Line="\t[errore]\t"+ repr(deb) + "\t"+ Line  
  return Line, EOL 
    
def AssegnaCoordinateRettangoli(x,y):
  Event1Top = y*(HEIGHT+YDIST)+TopMargin+YOFFSET
  Event1Left = x*(WIDTH+XDIST)+LeftMargin
  Event2Top = Event1Top+Event1Height
  Event2Left = x*(WIDTH+XDIST)+LeftMargin
  Event3Top = Event2Top+Event2Height
  Event3Left = x*(WIDTH+XDIST)+LeftMargin
  return Event1Top, Event1Left, Event2Top , Event2Left, Event3Top, Event3Left
  
def EstraiDati(Line,EOL):
  Comma1=Line.find("\t")
  Comma2=Line.find("\t",Comma1+1)
  Comma3=Line.find("\t",Comma2+1)
  Comma4=Line.find("\t",Comma3+1)
  Comma5=Line.find("\t",Comma4+1)
  Comma6=Line.find("\t",Comma5+1)
  Comma7=Line.find("\t",Comma6+1)
  Comma8=Line.find("\t",Comma7+1)
  Comma9=Line.find("\t",Comma8+1)
  Comma10=Line.find("\t",Comma9+1)
  Comma11=Line.find("\t",Comma10+1)
  Comma12=Line.find("\t",Comma11+1)
  Comma13=Line.find("\t",Comma12+1)
  Comma14=Line.find("\t",Comma13+1)
  StartTimes=Line.find("#") # Inizio elenco passaggi.
  Title=Line[0:Comma1]
  if (StartTimes>0): # Se ci sono passaggi multipli,
    Plot=Line[Comma13+1:StartTimes] # La trama termina dove iniziano i passaggi multipli (carattere speciale).
    Times= Line[StartTimes+1:EOL] # Gli altri passaggi devono essere gia' elencati alla fine della riga, dopo il carattere speciale.
  else: #altrimenti
    Plot=Line[Comma13+1:EOL] # La trama termina dove termina la riga.
    Times = " (nessuna replica)"
  Channel=Line[Comma2+1:Comma3]
  FirstDate=Line[Comma1+1:Comma2]
  FirstTime=Line[Comma1+1:Comma2] + " - " + Line[Comma6+1:Comma7] # Primo passaggio: quello della riga stessa.
  return Title,Plot,Channel,FirstTime,Times
  
  
def StampaHeaderFooter(PagesCount):
  global HeaderTop, HeaderLeft, HeaderWidth, HeaderHeight, FooterWidth, FooterHeight
  if (InScribus==1):
    HeaderText = createText(HeaderLeft, HeaderTop, HeaderWidth, HeaderHeight)
    setLineWidth(2, HeaderText)
    setLineColor("Black", HeaderText)
    setText("ELENCO FILM",HeaderText)
    setFontSize(20,HeaderText) 
    setTextAlignment(1,HeaderText) 
    
    FooterText = createText(FooterLeft, FooterTop, FooterWidth, FooterHeight)
    setLineWidth(2, FooterText)
    setLineColor("Black", FooterText)
    setText("Pag. " +  repr(PagesCount) + " - Note personali:",FooterText)
    setFontSize(12,FooterText)  
    setTextAlignment(0,FooterText)     
  else:
    print "HEADER=",HeaderLeft,HeaderTop,  HeaderWidth, HeaderHeight
    print "FOOTER=",FooterLeft,FooterTop,  FooterWidth, FooterHeight
      


if (InScribus==1):
    File1 =   file('breve.txt')
    FileContents = File1.read()    
    gotoPage(1)   
    if (OutputType=='film'):
      CreaFilmOutput()       
else:     
      File1 =   file('breve.txt')
      FileContents = File1.read()    
      if (OutputType=='film'):
        CreaFilmOutput() 
